import Vue from 'vue'
import Vuex from 'vuex'
import {getTimeInterval} from '../utils/index'
import {fetchSocial, fetchSiteInfo, fetchResources, fetchTags, fetchCategory} from '@/api'

Vue.use(Vuex)
// 略:后台获取系统运行时间
const runAt = '1589878800000';
let timer = null;
const state = {
    loading: false,
    runTimeInterval: '',
    socials: '',
    websiteInfo: ''
}
const mutations = {
    SET_LOADING: (state, v) => {
        state.loading = v;
    },
    SET_SOCIALS: (state, v) => {
        state.socials = v;
    },
    SET_SITE_INFO: (state, v) =>{
      state.websiteInfo = v;
    },
    SET_RESOURCES: (state, v) =>{
        state.resources = v;
    },
    SET_TAGS: (state, v) =>{
        state.tags = v;
    },
    SET_CATEGORY: (state, v) =>{
        state.category = v;
    },

    GET_RUNTIME_INTERVAL: (state) => {
        if (!timer || !state.runTimeInterval) {
            clearInterval(timer)
            timer = setInterval(() => {
                state.runTimeInterval = getTimeInterval(runAt);
            }, 1000);
        }
    }
}
const actions = {
    setLoading: ({commit}, v) => {
        commit('SET_LOADING', v);
    },
    initComputeTime: ({commit}) => {
        commit('GET_RUNTIME_INTERVAL');
    },
    getSiteInfo: ({commit,state}) =>{
        return new Promise(resolve => {
            if (state.websiteInfo){
                resolve(state.websiteInfo)
            }else {
                //缓存网站信息到sessionStorage
                if(sessionStorage.getItem("SITE_INFO")!=null){
                    commit('SET_SITE_INFO',JSON.parse(sessionStorage.getItem("SITE_INFO")));
                    resolve(JSON.parse(sessionStorage.getItem("SITE_INFO")));
                }else {
                    fetchSiteInfo().then(res => {
                        let data = res.data || {}
                        sessionStorage.setItem("SITE_INFO",JSON.stringify(data))
                        commit('SET_SITE_INFO',data);
                        resolve(data);
                    }).catch(err => {
                        resolve({});
                    })
                }

            }
        })
    },
    getSocials: ({commit,state}) =>{
        return new Promise((resolve => {
            if (state.socials){
                resolve(state.socials)
            } else {
                fetchSocial().then(res =>{
                    let data = res.data || []
                    commit('SET_SOCIALS',data);
                    resolve(data);
                }).catch(err =>{
                    resolve([]);
                })
            }
        }))
    },
    getResources: ({commit,state}) =>{
        return new Promise((resolve => {
            if (state.resources){
                resolve(state.resources)
            } else {
                fetchResources().then(res =>{
                    let data = res.data || []
                    commit('SET_RESOURCES',data);
                    resolve(data);
                }).catch(err =>{
                    resolve([]);
                })
            }
        }))
    },
    fetchTags: ({commit,state}) =>{
        return new Promise(resolve => {
            if (state.tags){
                resolve(state.tags)
            }else {
                //缓存网站信息到sessionStorage
                if(sessionStorage.getItem("TAGS")!=null){
                    commit('SET_TAGS',JSON.parse(sessionStorage.getItem("TAGS")));
                    resolve(JSON.parse(sessionStorage.getItem("TAGS")));
                }else {
                    fetchTags().then(res => {
                        let data = res.data || {}
                        sessionStorage.setItem("TAGS",JSON.stringify(data))
                        commit('SET_TAGS',data);
                        resolve(data);
                    }).catch(err => {
                        resolve({});
                    })
                }

            }
        })
    },
    fetchCategory: ({commit,state}) =>{
        return new Promise(resolve => {
            if (state.category){
                resolve(state.category)
            }else {
                //缓存网站信息到sessionStorage
                if(sessionStorage.getItem("CATEGORY")!=null){
                    commit('SET_CATEGORY',JSON.parse(sessionStorage.getItem("CATEGORY")));
                    resolve(JSON.parse(sessionStorage.getItem("CATEGORY")));
                }else {
                    fetchCategory().then(res => {
                        let data = res.data || {}
                        sessionStorage.setItem("CATEGORY",JSON.stringify(data))
                        commit('SET_CATEGORY',data);
                        resolve(data);
                    }).catch(err => {
                        resolve({});
                    })
                }

            }
        })
    },
}
const getters = {
    loading: state => state.loading,
    runTimeInterval: state => state.runTimeInterval,
    notice: state => state.websiteInfo?state.websiteInfo.notice:''
}
export default new Vuex.Store({
    state,
    mutations,
    actions,
    modules: {},
    getters
})
